<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Http\Models\Recommend;
use App\Http\Models\Post;
use App\Http\Models\PostCid;

class RecommendController extends Controller {

    public function index(Request $request)
    {
        //data不可数
        $data = Recommend::orderBy("created_at","desc")->get();
        //$data = $data->paginate(20);
        return view("admin.recommend.index",['data'=>$data,'active'=>'recommend']);
    }

    public function create(Request $request)
    {
        return view('admin.recommend.create',['active'=>'recommend']);
    }

    public function store(Request $request){
        $recommend = new Recommend;
        $recommend -> title = $request->input('title');
        $recommend -> position = intval($request -> input('position'));

        //唯一
        $hasPostion = Recommend::where(['position'=>$recommend->position])->count();
        if($hasPostion){
            return $this->failure('/admin/recommend','位置必须唯一');
        }

        $recommend -> save();
        return redirect('/admin/recommend');
    }

    public function edit($id){

        $data = Recommend::find($id);

        return view('admin.recommend.edit',['data'=>$data,'active'=>'recommend']);
    }


    public function update(Request $request,$id)
    {
        $recommend = Recommend::find($id);

        $recommend -> title = $request->input('title');
        $recommend -> position = intval($request -> input('position'));

        //唯一
        $hasPostion = Recommend::where(['position'=>$recommend->position])->where('id','<>',$recommend->id)->count();
        if($hasPostion){
            return $this->failure('/admin/recommend','位置必须唯一');
        }

        $recommend->save();

        return redirect('/admin/recommend');
    }

    public function delete($id)
    {
        $res = Recommend::destroy($id);
        if($res) return view('success',['url'=>'/admin/recommend']);

        return view('failure',['url'=>'/admin/recommend']);
    }

    public function append($id){

        //data不可数
        $data = Post::orderBy("created_at","desc");
        $data = $data->paginate(20);

        $cids = PostCid::all();

        $recommend = Recommend::find($id);
        $recommend->assoc = json_decode($recommend->assoc);
        $recommend->assoc = $recommend->assoc?$recommend->assoc:[];

        return view("admin.recommend.append",['data'=>$data,'cids'=>$cids,'recommend'=>$recommend,'active'=>'recommend']);
    }

    public function appendAction($recid,$postid){

        $recomend = Recommend::find($recid);
        $append = $recomend->assoc;
        if($append = json_decode($append)){
            if(in_array($postid,$append)){
                return response()->json(['state'=>'err','message'=>'请不要重复添加']);
            }
            array_push($append,$postid);
            $recomend->assoc = json_encode($append);
        }else{
            $recomend->assoc = json_encode([$postid]);
        }
        $recomend->save();

        return response()->json(['state'=>'ok','message'=>'添加成功']);
    }

    public function showAppend($id){

        $recommend = Recommend::find($id);
        $recommend->assoc = json_decode($recommend->assoc);
        $recommend->assoc = $recommend->assoc?$recommend->assoc:[];

        $data = Post::whereIn("id",$recommend->assoc)->get();

        return view("admin.recommend.showAppend",['data'=>$data,'recommend'=>$recommend,'active'=>'recommend']);

    }

    public function cancelAppend($recid,$postid){
        $recomend = Recommend::find($recid);
        $append = $recomend->assoc;
        if($append = json_decode($append)){
            $append = array_flip($append);
            unset($append[$postid]);
            $append = array_flip($append);
            $recomend->assoc = json_encode($append);
        }
        $recomend->save();
        return response()->json(['state'=>'ok','message'=>'取消成功']);
    }
}